package com.fsh.anime.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fsh.anime.mapper.FanjuMapper;
import com.fsh.anime.model.pojos.Fanju;
import com.fsh.anime.model.vos.AllSeasonVo;
import com.fsh.anime.model.vos.AllYearVo;
import com.fsh.anime.model.vos.Result;
import com.fsh.anime.service.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Autowired
    private FanjuMapper fanjuMapper;

    /**
     * 所有年加起来，按季度分组 ，每季度番剧总数
     * select season,count(*) as total from fanju group by season order by season asc
     * @return {@link Result}
     */
    @Override
    @Transactional
    public Result AllSeasonTotal() {
        QueryWrapper<Fanju> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("season,count(*) as total");
        queryWrapper.groupBy("season");
        queryWrapper.orderByAsc("season");
        List<Map<String,Object>> maps=fanjuMapper.selectMaps(queryWrapper);
        log.info("maps,{}",maps);
        return Result.success(maps);
    }

    /**
     * 各年总
     * select pub_time,count(*) as total from fanju group by pub_time order by pub_time desc
     * @return {@link Result}
     */
    @Override
    @Transactional
    public Result AllYearTotal() {
        //查询出数据
        QueryWrapper<Fanju> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("pub_time,count(*) as total");
        queryWrapper.groupBy("pub_time");
        queryWrapper.orderByDesc("pub_time");
        List<Map<String, Object>> maps = fanjuMapper.selectMaps(queryWrapper);

        //将年份和total各自装到集合中
        List<String> yearList=maps.stream().map(item->{
            return (String)item.get("pub_time");
        }).collect(Collectors.toList());
        List<Long> totalList=maps.stream().map(item->{
            return (Long)item.get("total");
        }).collect(Collectors.toList());

        AllYearVo allYearVo=new AllYearVo();
        allYearVo.setTotal(totalList);
        allYearVo.setYears(yearList);
        //返回数据
        return Result.success(allYearVo);
    }
}
